package per.com.controller;

import com.mchange.v2.sql.filter.SynchronizedFilterDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import per.com.bean.BaseBean;
import per.com.bean.UserBean;
import per.com.dao.UserDao;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.Vanilla.std;

@Controller
public class UserController {

    @Resource
    private UserDao userDao;

//    @ResponseBody
//    @RequestMapping(value = "/login")
//    public BaseBean<UserBean> login(String username, String password) {
//        UserBean userBean = userDao.login(username);
//        if (userBean != null) {
//            if (password.equals(userBean.getPassword())) {
//                return new BaseBean<UserBean>(0, "登录成功", userBean);
//            } else {
//                return new BaseBean<UserBean>(1, "密码不对，换一个试试");
//            }
//        } else {
//            return new BaseBean<UserBean>(2, "账号不存在，请注册");
//        }
//    }
//
//    @ResponseBody
//    @RequestMapping(value = "/reg")
//    public BaseBean<UserBean> reg(String username, String password) {
//        try {
//            userDao.reg(new UserBean(username, password));
//            return new BaseBean<UserBean>(0, "注册成功");
//        } catch (Exception e) {
//            return new BaseBean<UserBean>(1, "注册失败，用户名已存在");
//        }
//    }
//
//    @ResponseBody
//    @RequestMapping(value = "/change")
//    public BaseBean<UserBean> change(String username, String password) {
//        UserBean userBean = userDao.login(username);
//        if (userBean != null) {
//            try {
//                userBean.setPassword(password);
//                userDao.updatePsw(userBean);
//                return new BaseBean<UserBean>(0, "修改成功", userBean);
//            } catch (Exception e) {
//                return new BaseBean<UserBean>(1, "请检查网络，无法连接服务器", userBean);
//            }
//        } else {
//            return new BaseBean<UserBean>(2, "请输入正确的账号");
//        }
//    }

    /**
     * 查表
     */

    @RequestMapping("/webLogin")
    public String loginWeb(@RequestParam("username") String username,
                           @RequestParam("password") String password,Model model) {



        UserBean userBean = userDao.login(new UserBean(username,password));

        if (userBean != null&&userBean.getPassword().equals(password)&&userBean.getUsername().equals(username)){

            model.addAttribute("msg","登陆成功");
            return "teachermanage";

        }
        System.out.println(userBean);
        return "error";
    }
    @ResponseBody
    @RequestMapping(value = "/query")
    public List<UserBean> queryAllUser() {
        return userDao.queryAllUser();
    }

//    @ResponseBody
//    @RequestMapping(  value = "/save",method = RequestMethod.POST)
//    public Msg save(UserBean userBean Model model){
///**
// * 获取表单传来的数据
// * 对数据进行存储（userbean）
// * 将存储的数据发送给表单 使用ajax进行接受
// *
// */
//
//
//        return null;
//    }
}
